<template>
  <div class="list-container">
    <el-form ref="form" :model="form" label-width="200px">
      <el-form-item label="名称">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="android down url">
        <el-input v-model="form.android_down_url"></el-input>
      </el-form-item>
      <el-form-item label="android schemeurl">
        <el-input v-model="form.android_scheme_url"></el-input>
      </el-form-item>
      <el-form-item label="ios down url">
        <el-input v-model="form.ios_down_url"></el-input>
      </el-form-item>
      <el-form-item label="ios scheme url">
        <el-input v-model="form.ios_scheme_url"></el-input>
      </el-form-item>
      <el-form-item label="universal links">
        <el-input v-model="form.universal_links"></el-input>
      </el-form-item>
      <el-form-item label="安卓包名">
        <el-input v-model="form.package_name"></el-input>
      </el-form-item>

      <el-form-item label="安卓应用市场">
        <el-checkbox-group v-model="form.stores">
          <el-checkbox label="huawei">华为应用市场</el-checkbox>
          <el-checkbox label="xiaomi">小米应用商店</el-checkbox>
          <el-checkbox label="vivo">vivo应用商店</el-checkbox>
          <el-checkbox label="oppo">OPPO应用市场</el-checkbox>
          <el-checkbox label="honour">荣耀应用市场</el-checkbox>
          <el-checkbox label="yyb">应用宝</el-checkbox>
        </el-checkbox-group>
      </el-form-item>

      <el-form-item v-if="form.stores.includes('huawei')" label="华为应用市场URL">
        <el-input v-model="form.huawei_down_ur"></el-input>
      </el-form-item>

      <el-form-item v-if="form.stores.includes('xiaomi')" label="小米应用商店URL">
        <el-input v-model="form.xiaomi_down_ur"></el-input>
      </el-form-item>

      <el-form-item v-if="form.stores.includes('vivo')" label="vivo应用商店URL">
        <el-input v-model="form.vivo_down_ur"></el-input>
      </el-form-item>

      <el-form-item v-if="form.stores.includes('oppo')" label="OPPO应用市场URL">
        <el-input v-model="form.oppo_down_ur"></el-input>
      </el-form-item>

      <el-form-item v-if="form.stores.includes('honour')" label="荣耀应用市场URL">
        <el-input v-model="form.honour_down_ur"></el-input>
      </el-form-item>

      <el-form-item label="icon">
        <el-input v-model="form.icon"></el-input>
      </el-form-item>
      <el-form-item label="banner">
        <el-input v-model="form.banner"></el-input>
      </el-form-item>
      <el-form-item size="large">
        <el-button type="primary" @click="onSubmit">立即创建</el-button>
        <el-button @click="gotoConsole">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { appsCreateApi } from '@/api/apps';
export default {
  data() {
    return {
      form: {
        name: '',
        android_down_url: '',
        android_scheme_url: '',
        ios_down_url: '',
        ios_scheme_url: '',
        universal_links: '',
        package_name: '',
        icon: '',
        banner: '',
        stores: [],
        huawei_down_url: '',
        xiaomi_down_url: '',
        vivo_down_url: '',
        oppo_down_url: '',
        honour_down_url: '', // 新增荣耀商店 URL
        yyb_down_url: ''
      }
    };
  },
  methods: {
    gotoConsole(){
      this.$router.push('/console');
    },
    async onSubmit(){
      if(!this.form.name){
        return this.$message.error('请输入应用名称');
      }
      if(!this.form.android_down_url && !this.form.ios_down_url){
        return this.$message.error('至少上传一个下载地址');
      }

      // 验证已选应用商店的 URL
      for (const store of this.form.stores) {
        const urlKey = `${store}_down_ur`;
        if (!this.form[urlKey]) {
          return this.$message.error(`请输入${store}应用商店的URL`);
        }
      }

      let res = await appsCreateApi(this.form);
      if(res.code != 200){
        return this.$message.error(res.msg);
      }
      this.$message.success('创建成功');
      this.gotoConsole();
    }
  },
  watch: {
    'form.stores': {
      handler(newStores) {
        // 当取消选中某个商店时，清空对应的 URL
        const allStores = ['huawei', 'xiaomi', 'vivo', 'oppo', 'honour', 'yyb']; // 添加 honour
        allStores.forEach(store => {
          if (!newStores.includes(store)) {
            this.form[`${store}_down_url`] = '';
          }
        });
      },
      deep: true
    }
  }
};
</script>

<style scoped>
.list-container {
  padding: 20px;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

table {
  width: 100%;
  border-collapse: collapse;
}

</style>